//
//  SLCADemoViewModel.m
//  Objective-C
//
//  Created by 陆承东 on 2018/6/28.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLCADemoViewModel.h"

@implementation SLCADemoModelItem

+ (instancetype)itemWithTitle:(NSString *)title subTitle:(NSString *)subTitle routeK:(NSString *)routeK routeV:(NSString *)routeV {
    SLCADemoModelItem *item = [[SLCADemoModelItem alloc] init];
    
    item.title = title;
    item.subTitle = subTitle;
    item.routeK = routeK;
    item.routeV = routeV;
    
    return item;
}

@end

static NSString * const transformSectionK = @"变换";
static NSString * const appropriativeSectionK = @"专用图层";
static NSString * const implicitAnimationK = @"隐式动画";
static NSString * const explicitAnimationK = @"显式动画";
static NSString * const layerTimeK = @"图层时间";
static NSString * const bufferK = @"缓冲";
static NSString * const timerAnimationK = @"定时器动画";
static NSString * const efficientDrawK = @"高效绘图";
static NSString * const customDemoK = @"自定义";

@interface SLCADemoViewModel()

@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, strong) NSMutableDictionary *dataDict;

@end

@implementation SLCADemoViewModel

- (instancetype)init {
    self = [super init];
    
    if (self) {
        [self initData];
    }
    
    return self;
}

- (void)initData {
    self.sections = [NSMutableArray new];
    self.dataDict = [NSMutableDictionary new];
    
    NSMutableArray *dataArray = [NSMutableArray new];
    [self.sections addObject:transformSectionK];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"固体对象" subTitle:@"" routeK:@"g_demo_ca_solid_object" routeV:@""]];
    [self.dataDict setObject:[dataArray copy] forKey:transformSectionK];
    
    [self.sections addObject:appropriativeSectionK];
    dataArray = [NSMutableArray new];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CAShapeLayer" subTitle:@"" routeK:@"g_demo_ca_shape_layer" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CATextLayer" subTitle:@"" routeK:@"g_demo_ca_text_layer" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CATransformLayer" subTitle:@"" routeK:@"g_demo_ca_transform_layer" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CAGradientLayer" subTitle:@"" routeK:@"g_demo_ca_gradient_layer" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CAReplicatorLayer" subTitle:@"" routeK:@"g_demo_ca_replicator_layer" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CAScrollLayer" subTitle:@"" routeK:@"g_demo_ca_scroll_layer" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CAEmitterLayer" subTitle:@"" routeK:@"g_demo_ca_emitter_layer" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CAEAGLayer" subTitle:@"" routeK:@"g_demo_ca_eag_layer" routeV:@""]];
    [self.dataDict setObject:[dataArray copy] forKey:appropriativeSectionK];
    
    [self.sections addObject:implicitAnimationK];
    dataArray = [NSMutableArray new];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CATransaction" subTitle:@"" routeK:@"g_demo_ca_transaction" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CAAction" subTitle:@"" routeK:@"g_demo_ca_action" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CAPresentation&Model" subTitle:@"" routeK:@"g_demo_ca_pm" routeV:@""]];
    [self.dataDict setObject:dataArray forKey:implicitAnimationK];
    
    [self.sections addObject:explicitAnimationK];
    dataArray = [NSMutableArray new];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"PropertyAnimation" subTitle:@"" routeK:@"g_demo_ca_property_animation" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"AnimationGroup" subTitle:@"" routeK:@"g_demo_ca_animation_group" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"Transition" subTitle:@"" routeK:@"g_demo_ca_transition" routeV:@""]];
    [self.dataDict setObject:dataArray forKey:explicitAnimationK];
    
    [self.sections addObject:layerTimeK];
    dataArray = [NSMutableArray new];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"MediaTiming" subTitle:@"" routeK:@"g_demo_ca_media_timing" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"ManualAnimation" subTitle:@"" routeK:@"g_demo_ca_manual_animation" routeV:@""]];
    [self.dataDict setObject:dataArray forKey:layerTimeK];
    
    [self.sections addObject:bufferK];
    dataArray = [NSMutableArray new];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"Buffer" subTitle:@"" routeK:@"g_demo_ca_animation_speed" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CustomBuffer" subTitle:@"" routeK:@"g_demo_ca_custom_buffer" routeV:@""]];
    [self.dataDict setObject:dataArray forKey:bufferK];
    
    [self.sections addObject:timerAnimationK];
    dataArray = [NSMutableArray new];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"TimerFrame" subTitle:@"" routeK:@"g_demo_ca_timer_frame" routeV:@""]];
    [self.dataDict setObject:dataArray forKey:timerAnimationK];
    
    [self.sections addObject:efficientDrawK];
    dataArray = [NSMutableArray new];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"VectorDraw" subTitle:@"" routeK:@"g_demo_ca_vector_draw" routeV:@""]];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"DirtyRect" subTitle:@"" routeK:@"g_demo_ca_dirty_rect" routeV:@""]];
    [self.dataDict setObject:dataArray forKey:efficientDrawK];
    
    [self.sections addObject:customDemoK];
    dataArray = [NSMutableArray new];
    [dataArray addObject:[SLCADemoModelItem itemWithTitle:@"CustomDemo" subTitle:@"" routeK:@"g_demo_ca_custom_demo" routeV:@""]];
    [self.dataDict setObject:dataArray forKey:customDemoK];
}

- (NSInteger)numberOfSections {
    return self.sections.count;
}

- (NSInteger)numberOfRowsInSection:(NSInteger)section {
    if (section >= self.sections.count) {
        return 0;
    }
    
    NSString *sectionName = self.sections[section];
    NSArray *dataArray = self.dataDict[sectionName];
    if (!dataArray || ![dataArray isKindOfClass:[NSArray class]]) {
        return 0;
    }
    
    return dataArray.count;
}

- (id)dataForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section >= self.sections.count) {
        return nil;
    }
    
    NSString *sectionName = self.sections[indexPath.section];
    NSArray *dataArray = self.dataDict[sectionName];
    if (!dataArray || ![dataArray isKindOfClass:[NSArray class]] || indexPath.row >= dataArray.count) {
        return nil;
    }
    return dataArray[indexPath.row];
}

- (id)dataForSection:(NSInteger)section {
    if (section >= self.sections.count) {
        return nil;
    }
    
    return self.sections[section];
}

@end
